using System;
using System.Collections.Generic;
using System.Text;

namespace dender.dvdcover.core
{
    /// <summary>
    /// CoverBase defines the abstraction of an entity containing a width, 
    /// heigh , parts and lines
    /// </summary>
    public abstract class CoverBase  : DimensionBase
    {
        /// <summary>
        /// Gets of sets the cutting lines of a cover
        /// </summary>
        /// <exception cref="System.ArgumentNullException">
        /// Throws an ArgumentNullException if the collection is null
        /// </exception>
        private List<CuttingLine> mCuttingLines;
        public List<CuttingLine> CuttingLines
        {
            get
            {
                return mCuttingLines;
            }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("Lines");
                mCuttingLines = value;
            }
        }

        /// <summary>
        /// Gets or Sets the part of the cover
        /// </summary>
        /// <exception cref="System.ArgumentOutOfRangeException">
        /// Throws an ArgumentOutOfRangeException when a part is not located in a cover 
        ///</exception>
        private List<Part> mPart;
        public List<Part> Parts
        {
            get
            {
                return mPart;
            }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("Parts");
                if (value.Count > 0)
                {
                    foreach (Part p in value)
                    {
                        if (!CheckPartInCoverBase(p))
                            throw new ArgumentOutOfRangeException("Part");
                    }
                }
            }
        }

        /// <summary>
        /// Creates a CoverBase
        /// </summary>
        public CoverBase()
        {
            mCuttingLines = new List<CuttingLine>();
            mPart = new List<Part>();
        }

        /// <summary>
        /// Creates a CoverBase, initializeing width and height
        /// </summary>
        /// <param name="width">Width of the CoverBase</param>
        /// <param name="height">Heigh of the CoverBase</param>
        /// <exception cref="System.ArgumentException">
        /// Throws an argument exception if width or heigh are less then or equal
        /// to zero
        /// </exception>
        public CoverBase(float width, float height)
            : base(width, height)
        {
            mCuttingLines = new List<CuttingLine>();
            mPart = new List<Part>();
        }

        /// <summary>
        /// Copy Constructor
        /// </summary>
        /// <param name="copy"></param>
        public CoverBase(CoverBase copy) : this()
        {
            this.Height = copy.Height;
            this.Width = copy.Width;
            this.Parts.AddRange(copy.Parts);
            this.CuttingLines.AddRange(copy.CuttingLines);
        }

        /// <summary>
        /// Checks if a part is in the borders of a cover
        /// </summary>
        /// <param name="p">Part to check</param>
        /// <returns>True if the part is in the border of the cover</returns>
        public bool CheckPartInCoverBase(Part p)
        {
            return
                (
                    (p.Location.X >= 0 && p.Location.X + p.Width <= Width) &&
                    (p.Location.Y >= 0 && p.Location.Y + p.Height <= Height)
                );
        }
    }
}
